/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __EB_UART_H__
#define __EB_UART_H__

#include <kernel/device.h>
#include <kernel/driver.h>
#include <drivers/gpio.h>


struct uart_device;
struct uart_driver;


struct uart_ops {
    void (*start)(struct uart_device *device);
    void (*stop)(struct uart_device *device);
    void (*put_char)(struct uart_device *device, char ch);
    void (*put_string)(struct uart_device *device, char *str);
    char (*get_char)(struct uart_device *device);
    void (*set_baudrate)(struct uart_device *device, uint32_t baudrate);
};


struct uart_device {
    list_node_t  entry;
    const char  *compatible;
    struct uart_driver  *driver;
    struct gpio_conf *pin;
    uint32_t  pins;
    void  *io_base;
    uint32_t baudrate;
    void *tx_buf;
    void *rx_buf;
    uint32_t buf_size;
    uint32_t irq_num;
    void (*callback)(void);
};


struct uart_driver {
    list_node_t  entry;
    const char  *compatible;
    int (*probe)(struct uart_device *dev);
    void (*remove)(struct uart_device *dev);
    struct uart_ops *ops;
};


static inline void uart_put_char(struct uart_device *dev, char ch)
{
    struct uart_driver *drv = dev->driver;
    if(drv) {
        drv->ops->put_char(dev, ch);
    }
}


static inline void uart_put_string(struct uart_device *dev, char *str)
{
    struct uart_driver *drv = dev->driver;

    if(drv) {
        drv->ops->put_string(dev, str);
    }
}


static inline char uart_get_char(struct uart_device *dev)
{
    struct uart_driver *drv = dev->driver;
    if(drv) {
        return drv->ops->get_char(dev);
    }

    return 0;
}


int uart_device_register(struct uart_device *udev);
void uart_device_unregister(struct uart_device *udev);
int uart_driver_register(struct uart_driver *udrv);
void uart_driver_unregister(struct uart_driver *udrv);


void syslog_bind_uart_device(struct uart_device *dev);
struct uart_device* syslog_device(void);


#endif //!__EB_UART_H__
